<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    protected $fillable = [
        'type',
        'menu_group_id',
        'is_auth',
        'label',
        'route',
        'icon',
        'status',
        'sort',
        'pid'
    ];
    protected $casts = [
        'is_auth' => 'boolean',
        'status' => 'boolean'
    ];
    const TYPE_FIRST_MENU = 1;
    const TYPE_SECOND_MENU = 2;
    const TYPE_THIRD_MENU = 3;
    public static $attributesMap = [
        'pid'=>'父级',
        'type'=>'类型',
        'is_auth'=>'是否权限验证',
        'label'=>'菜单名称',
        'route'=>'路由',
        'icon'=>'图标',
        'status'=>'状态',
        'sort'=>'排序',
        'menu_group_id'=>'菜单组'
    ];
    public static function mapTypeMenu($hasKey = false)
    {
        if ($hasKey)
            return [
                self::TYPE_FIRST_MENU=>'一级菜单',
                self::TYPE_SECOND_MENU=>'二级菜单',
                self::TYPE_THIRD_MENU=>'三级菜单'
            ];
        else
            return [self::TYPE_FIRST_MENU,self::TYPE_SECOND_MENU,self::TYPE_THIRD_MENU];
    }

    //
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'menu_role');
    }

    public function permissions()
    {
        return $this->belongsToMany(Permission::class, 'menu_permission');
    }

    public function children()
    {
        return $this->hasMany(self::class, 'pid', 'id');
    }
}
